package com.ymatou.iapi.sellertrading.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellertrading.service.BatchAcceptOrderCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class Ts_BatchAcceptOrder {

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("订单无需补款");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new BatchAcceptOrderCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchAcceptOrder_001() {
		Logger.start(true, "买手批量接单");
		try {
			//创建订单并获取订单ID、买手ID、买家ID、需要支付的价格、tradingid
            Map map = new OrderCallService().createBatchM2cOrder(2);
            JSONArray OrderIds = (JSONArray) map.get("orderIds");
            // 支付订单
            int sellerId = (Integer) map.get("sellerId");
            int userId = (Integer) map.get("userId");
            int tradingId = (Integer) map.get("tradingId");
            // 计算所有订单支付定金
            BigDecimal totalPrice = new OrderCallService().getNeedPrePayAmountOfCashBymainOrderId(tradingId);
            // 订单支付
            new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);

			//把生成的订单号更新到脚本中
			Map<String,Object> a =  MapUtil.hashMap("OrderId", OrderIds.getInt(0), "UserId", userId);
			Map<String,Object> b =  MapUtil.hashMap("OrderId", OrderIds.getInt(1), "UserId", userId);
			java.util.List<Map<String, Object>> listmap = new ArrayList<>();
			listmap.add(a);
			listmap.add(b);
			//callandgetreturn
			JSONObject response  = new BatchAcceptOrderCall(sellerId, userId).callAndGetReturnData(listmap);			
			//验证返回值
			Logger.verifyEquals("操作成功", response.getString("Msg"), "操作成功");
			//验证sql
			DefaultWapper df = new DefaultWapper();
			Logger.verifyEquals(17, df.selectOrderByOderId(OrderIds.getInt(0)).get(0).get("iTradingStatus"), "订单表状态更新");
			Logger.verifyEquals(17, df.selectOrderByOderId(OrderIds.getInt(1)).get(0).get("iTradingStatus"), "订单表状态更新");
			//验证买手接单时间，误差不超过5秒
			AssertService.assertTime(YMTDateUtil.getDate(),YMTDateUtil.parseYMDHMSDate(df.selectOrderByOderId(OrderIds.getInt(0)).get(0).get("dAcceptTime").toString()),5);
			AssertService.assertTime(YMTDateUtil.getDate(),YMTDateUtil.parseYMDHMSDate(df.selectOrderByOderId(OrderIds.getInt(0)).get(0).get("dAcceptTime").toString()),5);
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchAcceptOrder_002() {
		Logger.start(false, "订单号不存在时批量接单失败");
		try {
			//创建订单并获取订单ID、买手ID、买家ID、需要支付的价格、tradingid
            ArrayList<Integer> list = new ArrayList<Integer>();
            Map map = new OrderCallService().createBatchM2cOrder(2);
            JSONArray OrderIds = (JSONArray) map.get("orderIds");
            // 支付订单
            int sellerId = (Integer) map.get("sellerId");
            int userId = (Integer) map.get("userId");
            int tradingId = (Integer) map.get("tradingId");
            // 计算所有订单支付定金
            BigDecimal totalPrice = new OrderCallService().getNeedPrePayAmountOfCashBymainOrderId(tradingId);
            // 订单支付
            new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);

			//把生成的订单号更新到脚本中
			Map<String,Object> a =  MapUtil.hashMap("OrderId", 123123, "UserId", userId);
			Map<String,Object> b =  MapUtil.hashMap("OrderId", OrderIds.getInt(1), "UserId", userId);
			java.util.List<Map<String, Object>> listmap = new ArrayList<>();
			listmap.add(a);
			listmap.add(b);
			//callandgetreturn
			JSONObject response  = new BatchAcceptOrderCall(sellerId, userId).callAndGetReturnData(listmap);			
			//验证返回值
			Logger.verifyEquals(201, response.get("Code"), "接口返回201");
			Logger.verifyEquals("部分订单接单失败", response.get("Msg"), "失败原因");
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchAcceptOrder_003() {
		Logger.start(false, "非订单所有者接单批量接单时失败");
		try {
			//创建订单并获取订单ID、买手ID、买家ID、需要支付的价格、tradingid
            ArrayList<Integer> list = new ArrayList<Integer>();
            Map map = new OrderCallService().createBatchM2cOrder(2);
            JSONArray OrderIds = (JSONArray) map.get("orderIds");
            // 支付订单
            int sellerId = (Integer) map.get("sellerId");
            int userId = (Integer) map.get("userId");
            int tradingId = (Integer) map.get("tradingId");
            // 计算所有订单支付定金
            BigDecimal totalPrice = new OrderCallService().getNeedPrePayAmountOfCashBymainOrderId(tradingId);
            // 订单支付
            new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);

			//把生成的订单号更新到脚本中
			Map<String,Object> a =  MapUtil.hashMap("OrderId", OrderIds.getInt(0), "UserId", userId);
			Map<String,Object> b =  MapUtil.hashMap("OrderId", OrderIds.getInt(1), "UserId", userId);
			java.util.List<Map<String, Object>> listmap = new ArrayList<>();
			listmap.add(a);
			listmap.add(b);
			//callandgetreturn
			int sellerid1 = sellerId + 1; 
			JSONObject response  = new BatchAcceptOrderCall(3383, userId).callAndGetReturnData(listmap);			
			//验证返回值
			Logger.verifyEquals(201, response.get("Code"), "接口返回201");
			Logger.verifyEquals("部分订单接单失败", response.get("Msg"), "失败原因");
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchAcceptOrder_004() {
		Logger.start(false, "没有付款的订单不能接单");
		try {
			//创建订单并获取订单ID、买手ID、买家ID、需要支付的价格、tradingid
            ArrayList<Integer> list = new ArrayList<Integer>();
            Map map = new OrderCallService().createBatchM2cOrder(2);
            JSONArray OrderIds = (JSONArray) map.get("orderIds");
            // 支付订单
            int sellerId = (Integer) map.get("sellerId");
            int userId = (Integer) map.get("userId");
            int tradingId = (Integer) map.get("tradingId");

			//把生成的订单号更新到脚本中
			Map<String,Object> a =  MapUtil.hashMap("OrderId", OrderIds.getInt(0), "UserId", userId);
			Map<String,Object> b =  MapUtil.hashMap("OrderId", OrderIds.getInt(1), "UserId", userId);
			java.util.List<Map<String, Object>> listmap = new ArrayList<>();
			listmap.add(a);
			listmap.add(b);
			//callandgetreturn
			JSONObject response  = new BatchAcceptOrderCall(sellerId, userId).callAndGetReturnData(listmap);			
			//验证返回值
			Logger.verifyEquals(201, response.get("Code"), "接口返回201");
			Logger.verifyEquals("部分订单接单失败",response.get("Msg"), "错误原因");
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}
	
	
	
	
	
}
